package com.lgt_enterprise_android.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.lgt_enterprise_android.BuildConfig;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.utils.DialogUtils;
import com.util.AppUtil;


public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";
    protected Dialog mLoadingDialog;

    private ImmersionBar mImmersionBar;

    protected int statusBarColor = R.color.colorPrimary;
    protected boolean isDarkfont = true;

    protected Context mContext;

    protected int dp2Px(int _dp){
        return (int)(AppUtil.getdensityDpi(mContext)*_dp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mContext=this;

        if(BuildConfig.DEBUG){
            Log.i("AAAAAA","        "+this.getClass().getSimpleName()+" ------ onCreate");
        }

        if(!getClass().getSimpleName().equals("ArrangeJobActivity")){
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.fitsSystemWindows(true);

            if (statusBarColor != R.color.white) {
                isDarkfont = false;
            }
            mImmersionBar.statusBarColor(statusBarColor, 0.2f).statusBarDarkFont(isDarkfont, 0.2f).init();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        );
    }

    protected void log(String msg){
        if(BuildConfig.DEBUG){
            Log.i(this.getClass().getSimpleName(),msg);
        }
    }
    protected void log(int msg){
        log(msg+"");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(BuildConfig.DEBUG){
            Log.i("AAAAAA","        "+this.getClass().getSimpleName()+" ------ onDestroy");
        }

        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }//必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态

        if(mLoadingDialog!=null){
            mLoadingDialog.dismiss();
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public void showLoadingDialog() {
        showLoadingDialog(true,"");
    }

    public void showLoadingDialog(String msg) {
        showLoadingDialog(true,msg);
    }

    public void showLoadingDialog(boolean isCancle, String message) {
        if (mLoadingDialog == null) {
            mLoadingDialog = DialogUtils.getProgressDialog(this, message);
        }

        mLoadingDialog.setCanceledOnTouchOutside(isCancle);
        mLoadingDialog.setCancelable(isCancle);

        if (!isFinishing()) {
            mLoadingDialog.show();
        }
    }
//
//
    public void dismissLoadingDialog() {
        if (mLoadingDialog != null && !isFinishing()) {
            mLoadingDialog.dismiss();
        }
    }


}
